import {checkNickname, checkPassword, checkSmsCode, dateFormat, getHttpPath} from '../../js/my-utils'
import {showMessage} from '../../js/my-plugin'

// 路径列表
export const pathArr = {home: 0, big: 1, info: 2, avatar: 3, coins: 4, diamonds: 5, record: 6}
// 标题列表
export const title = ['首页', '大会员', '我的信息', '我的头像', '我的硬币', '我的钻石', '我的记录']
// 填充用户信息
export const getInfo = (_this, user) => {
  if (_this.$store.state.user == null || _this.$store.state.user === '') {
    showMessage('您还没有登录', 0)
    _this.$router.push('/login')
    return
  }
  // 填充用户信息
  _this.user.id = user.id
  _this.user.username = user.username
  _this.user.nickname = user.nickname
  _this.user.avatar = getHttpPath(user.avatar)
  _this.user.signature = user.signature
  _this.user.birth = user.birth == null ? '' : dateFormat('yyyy-mm-dd', new Date(user.birth))
  _this.user.tag = user.tag
  _this.user.vipExpiredTime = user.vipExpiredTime == null ? new Date() : new Date(user.vipExpiredTime)
  _this.user.isVip = false
  const authorities = user.authorities
  for (let auth of authorities) {
    if (auth.authority === 'VIP') {
      _this.user.isVip = true
    }
  }
}
// 修改用户信息
export const setInfo = function (_this, user) {
  if (!checkNickname(user.nickname)) {
    showMessage('昵称长度必须为2-16个字符，不可包含-和_以外的特殊字符', 0)
    return
  }
  const birth = user.birth
  const data = {
    nickname: user.nickname,
    signature: user.signature,
    sex: user.sex,
    birthday: birth === '' ? null : new Date(birth).getTime(),
    _method: 'put'
  }
  _this.$axios({
    method: 'post',
    url: '/api/user/info',
    data: _this.$qs.stringify(data),
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      console.log(res.data)
      if (res.data.code === 200) {
        showMessage(res.data.msg, 1)
        _this.reload()
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.data
        window.localStorage.setItem('authorization', res.data.data)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage('登陆失败，无法连接服务器', 0)
    })
}

// 充值
export const pay = function (_this) {
  _this.$axios({
    method: 'get',
    url: '/api/user/pay',
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      console.log(res.data)
      if (res.data.code === 200) {
        // 更新token
        _this.authorization = res.data.data
        window.localStorage.setItem('authorization', res.data.data)
        showMessage(res.data.msg, 1)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage('登陆失败，无法连接服务器', 0)
    })
}

// 修改密码
export const changePassword = function (_this) {
  if (!checkPassword(_this.password)) {
    showMessage('密码格式错误', 0)
    return
  }
  if (!checkSmsCode(_this.smsCode)) {
    showMessage('验证码应为6位数字', 0)
    return
  }
  const data = {
    mobile: _this.user.username,
    password: _this.password,
    smsCode: _this.smsCode,
    _method: 'put'
  }
  _this.$axios({
    method: 'post',
    url: '/api/user/change_password',
    data: _this.$qs.stringify(data),
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      console.log(res.data)
      if (res.data.code === 200) {
        showMessage(res.data.msg, 1)
        _this.authorization = ''
        _this.$store.commit('setUser', '')
        window.localStorage.setItem('authorization', '')
        _this.$router.push('/login')
        // _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage('登陆失败，无法连接服务器', 0)
    })
}
